package com.itany.life.userdemo.controller;

import com.github.pagehelper.PageInfo;
import com.itany.life.userdemo.entity.User;
import com.itany.life.userdemo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {


    @Autowired
    private UserService userService;

    @RequestMapping("/find")
    public Map<String,Object> find(@RequestParam(value = "page",defaultValue = "1") Integer page,
                                   @RequestParam(value = "rows",defaultValue = "10") Integer rows,
                                   User user){
        System.out.println("UserController.find()");

        System.out.println("page = " + page);
        System.out.println("rows = " + rows);

        PageInfo<User> pageInfo=userService.find(page,rows,user);


        Map<String,Object> data=new HashMap<>();
        data.put("total",pageInfo.getTotal());
        data.put("rows",pageInfo.getList());

        return data;


    }


    @RequestMapping("/modifyFlag")
    public String modifyFlag(Integer id,Integer flag){
        userService.modifyFlag(id,flag);
        return "SUCCESS";
    }

    @RequestMapping("/findById")
    public User findById(Integer id){
        User user=userService.findById(id);

        user.setTreeIds(new Integer[]{12,21,22,4});

        return user;
    }



    @RequestMapping("/getComboBox")
    public List<Map<String,Object>> getComboBox(){
        List<Map<String,Object>> list=new ArrayList<>();
        for (int i = 0; i < 4; i++) {
            Map<String,Object> map=new HashMap<>();
            map.put("id",i+1);
            map.put("text","text"+(i+1));
            list.add(map);
        }
        return list;
    }

    @RequestMapping("/modify")
    public String modify(User user){
        System.out.println("user = " + user);
        userService.modify(user);
        return "SUCCESS";
    }


    @RequestMapping("/getTree")
    public List<Map<String,Object>> getTree(String id){
        List<Map<String,Object>> list=new ArrayList<>();

        if(id==null||"".equals(id)){

            for (int i = 0; i < 5; i++) {
                Map<String,Object> node=new HashMap<>();
                node.put("id",(i+1));
                node.put("text","node"+(i+1));
                node.put("state","closed");
                list.add(node);
            }
            return list;

        }

        for (int i = 0; i < 3; i++) {
            Map<String,Object> node=new HashMap<>();
            node.put("id",id+(i+1));
            node.put("text","children"+id+(i+1));
            node.put("state","open");
            list.add(node);
        }

        return list;


    }













}
